package com.gxks.lhs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxks.lhs.entity.Favorite;
import com.gxks.lhs.entity.Goods;
import com.gxks.lhs.mapper.FavoriteMapper;
import com.gxks.lhs.service.IFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 收藏表 服务实现类
 * </p>
 *
 * @author LuoHaisong
 * @since 2023-05-18
 */
@Service
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, Favorite> implements IFavoriteService {
    @Autowired
    private FavoriteMapper favoriteMapper;

    @Override
    public boolean addOrCancelFavorite(String userId, String goodsId) {
        String id = userId + goodsId;
        System.out.println("收藏id" + id);
        if (this.getById(id) != null) {
            return this.removeById(userId + goodsId);
        } else {
            Favorite favorite = new Favorite();
            favorite.setId(id).setGoodsId(goodsId).setUserId(userId);
            return this.save(favorite);
        }
    }

    @Override
    public boolean isFavorite(String userId, String goodsId) {
        log.warn(String.valueOf(this.getById(userId + goodsId) != null));
        log.warn(String.valueOf(this.getById(userId + goodsId)));
        return this.getById(userId + goodsId) != null;
    }

    @Override
    public Page<Goods> getFavoriteList(String userId, Integer page, Integer size) {
        Page<Goods> goodsPage = new Page<>(page, size);
        List<Goods> goods = favoriteMapper.getFavoriteList(userId, page, size);
        return goodsPage.setRecords(goods);
    }

    @Override
    public boolean deleteFavoriteByIds(List ids) {
        return this.removeByIds(ids);
    }
}
